using Microsoft.AspNetCore.Mvc;
using JTZY.Model.Business.Dto;
using JTZY.Model.Business;
using JTZY.Service.Business.IBusinessService;
using JTZY.Admin.WebApi.Filters;
using Aliyun.OSS;
using JTZY.Service.Business;

//创建时间：2024-10-27
namespace JTZY.Admin.WebApi.Controllers.Business
{
    /// <summary>
    /// 用户表
    /// </summary>
    [Verify]
    [Route("business/Corecmsuser")]
    public class CorecmsuserController : BaseController
    {
        /// <summary>
        /// 用户表接口
        /// </summary>
        private readonly ICorecmsuserService _CorecmsuserService;
        private readonly ICorecmsagentService _CorecmsagentrService;

        public CorecmsuserController(ICorecmsuserService CorecmsuserService, ICorecmsagentService corecmsagentrService)
        {
            _CorecmsuserService = CorecmsuserService;
            _CorecmsagentrService = corecmsagentrService;
        }

        /// <summary>
        /// 查询用户表列表
        /// </summary>
        /// <param name="parm"></param>
        /// <returns></returns>
        [HttpGet("list")]
        [ActionPermissionFilter(Permission = "corecmsuser:list")]
        public IActionResult QueryCorecmsuser([FromQuery] CorecmsuserQueryDto parm)
        {
            parm.ParentId = HttpContext.GetUId();
            var response = _CorecmsuserService.GetList(parm);
            return SUCCESS(response);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="parm"></param>
        /// <returns></returns>
        [HttpGet("approvelist")]
        [ActionPermissionFilter(Permission = "approve:list")]
        public IActionResult QueryApproveCorecmsuser([FromQuery] CorecmsuserQueryDto parm)
        {
            parm.ParentId = HttpContext.GetUId();
            var response = _CorecmsuserService.GetList(parm);
            return SUCCESS(response);
        }

        /// <summary>
        /// 查询用户表详情
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        [HttpGet("{Id}")]
        public IActionResult GetCorecmsuser(int Id)
        {
            var response = _CorecmsuserService.GetInfo(Id);
            var agent=_CorecmsagentrService.Queryable().Includes(x => x.CorecmsagentgradeNav) //填充子对象
.Where(a => a.UserId == Id).First();
            var info = response.Adapt<CorecmsuserDto>();

            if (agent != null) 
            {
                info.Agent = agent;
            }
            return SUCCESS(info);
        }

        /// <summary>
        /// 添加用户表
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [ActionPermissionFilter(Permission = "corecmsuser:add")]
        [Log(Title = "用户表", BusinessType = BusinessType.INSERT)]
        public IActionResult AddCorecmsuser([FromBody] CorecmsuserDto parm)
        {

            var oldUser = _CorecmsuserService.Queryable().Where(p => p.AccountCode == parm.AccountCode).First();
            if (oldUser != null)
            {
                //throw new Exception("账号已存在");
                return ToResponse(ResultCode.FAIL, "账号已存在");
            }
            var modal = parm.Adapt<Corecmsuser>().ToCreate(HttpContext);
            modal.ParentId = HttpContext.GetUId();
            var parent = _CorecmsuserService.GetById(modal.ParentId);
            modal.IsReviewed = 1;
            modal.IsDelete = false;
            modal.NickName = modal.UserName;
            modal.CreateTime = DateTime.Now;
            modal.Status = 1;
            modal.StoreId = parent.StoreId;
            modal.PassWord = Tools.EnPassword("123456", modal.CreateTime);
            modal.IsOpenAdmin = false;
            var response = _CorecmsuserService.AddCorecmsuser(modal);
            if (response != null)
            {
                var user = _CorecmsuserService.GetById(response.Id);
                _CorecmsuserService.ResetPwd(response.Id, Tools.EnPassword("123456", user.CreateTime));
            }
            return SUCCESS(response);
        }

        /// <summary>
        /// 更新用户表
        /// </summary>
        /// <returns></returns>
        [HttpPut]
        [ActionPermissionFilter(Permission = "corecmsuser:edit")]
        [Log(Title = "用户表", BusinessType = BusinessType.UPDATE)]
        public IActionResult UpdateCorecmsuser([FromBody] CorecmsuserDto parm)
        {
            var modal = parm.Adapt<Corecmsuser>().ToUpdate(HttpContext);
            modal.NickName = modal.UserName;
            var response = _CorecmsuserService.UpdateCorecmsuser(modal);

            return ToResponse(response);
        }

        [HttpPost("appprove")]
        [ActionPermissionFilter(Permission = "approve:edit")]
        [Log(Title = "用户表", BusinessType = BusinessType.UPDATE)]
        public IActionResult Approve([FromBody] CorecmsuserDto parm)
        {
            var modal = parm.Adapt<Corecmsuser>().ToUpdate(HttpContext);
            var response = _CorecmsuserService.ApproveUser(modal);

            return ToResponse(response);
        }

        /// <summary>
        /// 删除用户表
        /// </summary>
        /// <returns></returns>
        [HttpPost("delete/{ids}")]
        [ActionPermissionFilter(Permission = "corecmsuser:delete")]
        [Log(Title = "用户表", BusinessType = BusinessType.DELETE)]
        public IActionResult DeleteCorecmsuser([FromRoute]string ids)
        {
            var idArr = Tools.SplitAndConvert<long>(ids);

            return ToResponse(_CorecmsuserService.DeleteCorecmsuser(idArr));
        }

        [HttpPost("resetpwd")]
        [ActionPermissionFilter(Permission = "approve:edit")]
        [Log(Title = "用户表", BusinessType = BusinessType.UPDATE)]
        public IActionResult ResetPwd([FromBody] CorecmsuserDto parm)
        {
            var modal = parm.Adapt<Corecmsuser>().ToUpdate(HttpContext);
            var response = _CorecmsuserService.ResetPwd(modal.Id, Tools.EnPassword("123456", modal.CreateTime));

            return ToResponse(response);
        }
    }
}